#!/usr/bin/env python3
import sys
import os
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from frontend.main_window import MainWindow
from PyQt6.QtWidgets import QApplication
from loguru import logger
from config.settings import get_settings


def setup_logging():
    """配置日志"""
    settings = get_settings()
    
    # 创建日志目录
    log_dir = Path(settings.log_file).parent
    log_dir.mkdir(exist_ok=True)
    
    # 配置loguru
    logger.add(
        settings.log_file,
        rotation="10 MB",
        retention="10 days",
        level=settings.log_level,
        format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name}:{function}:{line} | {message}"
    )


def main():
    """主函数"""
    try:
        # 初始化日志
        setup_logging()
        logger.info("启动UI自动化管理工具")
        
        # 创建PyQt6应用
        app = QApplication(sys.argv)
        
        # 创建主窗口
        window = MainWindow()
        window.show()
        
        # 运行应用
        sys.exit(app.exec())
        
    except Exception as e:
        logger.error(f"应用启动失败: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()